<template>
  <div class="product__item">
    <slot name="chenckedSlot"></slot>
    <img
      :src="item.imgUrl"
      alt=""
      :class="
        product__item__img__cart
          ? 'product__item__img__cart'
          : 'product__item__img'
      "
    />
    <div class="product__item__content">
      <h4 class="product__item__header">{{ item.name }}</h4>
      <p class="product__item__sale" v-if="!show">月售{{ item.sales }}件</p>
      <div
        :class="
          product__item__price__cart
            ? 'product__item__price__cart'
            : 'product__item__price'
        "
      >
        <template v-if="!year">
          <span class="product__item__now">&yen;</span>{{ item.price }}
          <span class="product__item__past">&yen;{{ item.oldPrice }}</span>
        </template>
        <template v-else>
          <span class="product__item__now">&yen;</span
          >{{ item.price }}&nbsp;x&nbsp;
          {{ item.count }}
        </template>
      </div>
    </div>
    <div class="product__number">
      <template v-if="!number">
        <span
          class="product__number__reduce iconfont"
          @click="productChangeClick(id, item._id, item, -1, name)"
          v-if="getNum(id, item._id) > 0"
          >&#xe8e2;</span
        >
        {{ getNum(id, item._id) ? getNum(id, item._id) : "" }}
        <span
          class="product__number__add iconfont"
          @click="productChangeClick(id, item._id, item, +1, name)"
          >&#xe889;</span
        >
      </template>
      <template v-else>
        <div class="product__number">
          &yen;{{ (item.price * item.count).toFixed(2) }}
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { useCommodityEffect } from "../cartCommon/commonCartEffect";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
// import { inject } from "vue";
const getNum = (shopId, productId) => {
  const store = useStore();
  const num = store.state.cartList?.[shopId]?.productList?.[productId]?.count;
  return num;
};
export default {
  name: "commodityPage",
  props: [
    "show",
    "item",
    "product__item__img__cart",
    "product__item__price__cart",
    "number",
    "year",
    "name",
  ],
  setup() {
    const store = useStore();
    const route = useRoute();
    const id = route.params.id;
    const cartList = store.state.cartList;
    const { productChangeClick } = useCommodityEffect(); //
    return { productChangeClick, id, cartList, getNum };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";

.product__item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.12rem 0;
  border-bottom: 0.01rem solid $content-bgColor;
  &__img {
    width: 0.68rem;
    height: 0.68rem;
    &__cart {
      width: 0.46rem;
      height: 0.46rem;
    }
  }
  &__content {
    flex: 1;
    margin-left: 0.16rem;
    overflow-x: hidden;
  }
  &__header {
    width: 100%;
    margin: 0;
    font-size: 0.14rem;
    line-height: 0.2rem;
    color: $content-fontcolor;
    padding-right: 0.2rem;
    @include ellipsis;
  }
  &__sale {
    margin: 0.06rem 0;
    font-size: 0.12rem;
  }
  &__price {
    font-size: 0.14rem;
    color: $color-e93b;
    line-height: 0.2rem;
    &__cart {
      font-size: 0.14rem;
      color: $color-e93b;
      line-height: 0.2rem;
      margin-top: 0.1rem;
    }
  }

  &__now {
    font-size: 0.12rem;
  }
  &__past {
    font-size: 0.12rem;
    color: $color-999;
    text-decoration: line-through;
    margin-left: 0.12rem;
  }
  .product__number {
    line-height: 0.68rem;
    font-size: 0.14rem;
    color: #000;
    &__reduce,
    &__add {
      display: inline-block;
      width: 0.17rem;
      height: 0.17rem;
      border-radius: 50%;
      text-align: center;
      line-height: 0.17rem;
      font-size: 0.14rem;
    }
    &__reduce {
      border: 0.01rem solid $color-666;
      color: $color-666;
      margin-right: 0.05rem;
    }
    &__add {
      background-color: $color-0091ff;
      color: $color-fff;
      margin-left: 0.05rem;
    }
  }
}
</style>